package edu.buaa.common.utils;

import java.util.*;

public class PFieldList {
    private final Map<String, List<PVal>> data = new HashMap<>();

    public int add(String key, PVal v){
        List<PVal> lst = data.get(key);
        if(lst==null){
            lst = new LinkedList<>();
            lst.add(v);
            data.put(key, lst);
        }else{
            lst.add(v);
        }
        return lst.size();
    }

    public int size(){
        int size = -1;
        for(String k : data.keySet()){
            if(size==-1) size = data.get(k).size();
            else assert size == data.get(k).size():"expect size="+size+" but got "+data.get(k).size()+" instead.";
        }
        return size;

    }

    public Set<String> keys(){
        return data.keySet();
    }

    public Set<String> keysWithout(String... exclude){
        Set<String> s = new HashSet<>(data.keySet());
        s.removeAll(Arrays.asList(exclude));
        return s;
    }

    public PVal get(String key, int index) {
        List<PVal> lst = data.get(key);
        if(lst==null) throw new IllegalStateException("key "+key+" not found in PFieldList");
        else return lst.get(index);
    }
}
